package com.nhjf.mobile.controller.comment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.command.comment.CreateCoursesCommentCommand;
import com.nhjf.command.comment.CreateFinetTalkCommentCommand;
import com.nhjf.command.comment.CreateFollowThePaintingCommentCommand;
import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.finetTalk.FinetTalkComment;
import com.nhjf.model.followThePainting.FollowThePaintingComment;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.CoursesComment;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.finetTalk.FinetTalkCommentQo;
import com.nhjf.pojo.qo.followThePainting.FollowThePaintingCommentQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.CoursesCommentQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.pojo.qo.total.CourseCountQo;
import com.nhjf.service.finetTalk.FinetTalkCommentService;
import com.nhjf.service.followThePainting.FollowThePaintingCommentService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.CoursesCommentService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

/**
 * @类功能说明：精讲评论
 * @部门：技术部
 * @作者：龚一凡
 * @版本：V1.0
 */
@Controller
@RequestMapping(value = "/followThePaintingComment")
public class FollowThePaintingCommentController {

	@Autowired
	private FollowThePaintingCommentService followThePaintingCommentService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private StudentService studentService;

	/**
	 * 
	 * @方法功能说明：跟着画评论
	 * @修改者名字：龚一凡
	 * @修改内容：
	 * @参数：
	 * @return:
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/queryFollowThePaintingComment")
	public String queryFollowThePaintingComment(HttpServletRequest request, Model model,@RequestParam(value="followThePaintingId", required=true) String followThePaintingId,@RequestParam(value="page", defaultValue="1")Integer page){

		Map<String, Object> resMap=new HashMap<String, Object>();
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		FollowThePaintingCommentQo qo=new FollowThePaintingCommentQo();
		qo.setSeachUser(true);
		qo.setFollowThePaintingId(Long.parseLong(followThePaintingId));
		pagination.setCondition(qo);
		pagination.setPageNo(page);
		pagination = followThePaintingCommentService.queryPagination(pagination);
		List<FollowThePaintingComment> commentList=(List<FollowThePaintingComment>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(FollowThePaintingComment comment:commentList) {
			if(comment.getUser()!=null) {
				comment.getUser().setUserPassword("");
			}
			
//			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String fom= format1.format(comment.getCommentTime());
			comment.setCommentTimeString(fom);
			if(comment.getCommentUserType()==1) {
				TeachersQo teacherQo=new TeachersQo();
				teacherQo.setUserId(comment.getUser().getId());
				Teacher teacher=teachersService.queryUnique(teacherQo);
				if(teacher!=null) {
					
					comment.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
					comment.setNickname(teacher.getNickname());
				}
			}else {
				StudentQo studentQo=new StudentQo();
				studentQo.setUserid(comment.getUser().getId());
				Student student=studentService.queryUnique(studentQo);
				if(student!=null) {
					comment.setAvatar(imagePath+"/picture/"+student.getAvatar());
					comment.setNickname(student.getNickname());
				}
			}
			
		}
		resMap.put("code", "200");
		resMap.put("message", pagination);
		return JSONObject.fromObject(resMap).toString();
	}

	/**
	 * 
	 * @方法功能说明：跟着画评论创建
	 * @修改者名字：龚一凡
	 * @修改内容：
	 * @参数：
	 * @return:
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/createFollowThePaintingComment")
	public String createFollowThePaintingComment(@RequestParam(value="token", required=true)String  token,HttpServletRequest request, Model model,String followThePaintingId,String comment){

		Map<String, Object> resMap=new HashMap<String, Object>();

		if(StringUtils.isEmpty(token)) {
			resMap.put("code", "406");
			resMap.put("message", "未登录!");
			return JSONObject.fromObject(resMap).toString();
		}
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			adminSession.getLoginName();
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {

				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				CreateFollowThePaintingCommentCommand command=new CreateFollowThePaintingCommentCommand();
				if(StringUtils.isEmpty(comment)) {
					resMap.put("code", "500");
					resMap.put("message", "评论不能为空");
					return JSONObject.fromObject(resMap).toString();
				}
				if(StringUtils.isEmpty(followThePaintingId)) {
					resMap.put("code", "500");
					resMap.put("message", "评论跟着画不能为空");
					return JSONObject.fromObject(resMap).toString();
				}
				command.setFollowThePaintingId(Long.parseLong(followThePaintingId));
				command.setComment(comment);
				command.setUserId(user.getId());
				command.setCommentType(1);
				TeachersQo teachersQo=new TeachersQo();
				teachersQo.setUserId(user.getId());
				Teacher teacher= teachersService.queryUnique(teachersQo);
				if(teacher==null) {
					command.setCommentUserType(2);
				}else {
					command.setCommentUserType(1);
				}
				ServiceResultDto dto=followThePaintingCommentService.createFollowThePaintingComment(command);
				if(dto.getSuccess()) {
					resMap.put("code", "200");
					resMap.put("message", "评论成功");
					return JSONObject.fromObject(resMap).toString();
				}else {
					resMap.put("code", "500");
					resMap.put("message", dto.getResultMessage());
					return JSONObject.fromObject(resMap).toString();
				}
			

			}
		}

	}
}
